<?php
/*
  [EWEISHOP] Copyright(C) 2017 Elinkint Inc.
  This is NOT a freeware, use is subject to license terms
   
  OfficialAccountCustomerService.php 2018-05-25 17:26 likexin
   
*/

namespace common\library\wechat\officialaccount;

use common\library\wechat\WechatApi;
use common\component\wechat\Wechat;

class OfficialAccountCustomerService extends WechatApi
{

    /**
     * 获取客服列表
     * @return mixed
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/customer_service
     */
    public static function getList()
    {
        $result = self::getInstance()->customer_service->list();
        return Wechat::apiError($result);
    }

    /**
     * 获取在线客服列表
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/customer_service
     */
    public static function getOnline()
    {
        $result = self::getInstance()->customer_service->online();
        return Wechat::apiError($result);
    }

    /**
     * 添加客服
     * @param string $account 客服账号
     * @param string $nickname 客服昵称
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/customer_service
     */
    public static function create(string $account, string $nickname)
    {
        $result = self::getInstance()->customer_service->create($account, $nickname);
        return Wechat::apiError($result);
    }

    /**
     * 修改客服
     * @param string $account 客服账号
     * @param string $nickname 客服昵称
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/customer_service
     */
    public static function update(string $account, string $nickname)
    {
        $result = self::getInstance()->customer_service->update($account, $nickname);
        return Wechat::apiError($result);
    }

    /**
     * 删除客服
     * @param string $account 客服账号
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/customer_service
     */
    public static function delete(string $account)
    {
        $result = self::getInstance()->customer_service->delete($account);
        return Wechat::apiError($result);
    }

    /**
     * 设置客服头像
     * @param string $account 客服账号
     * @param string $avatarPath 本地图片路径
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/customer_service
     */
    public static function setAvatar(string $account, string $avatarPath)
    {
        $result = self::getInstance()->customer_service->setAvatar($account, $avatarPath);
        return Wechat::apiError($result);
    }

    /**
     * 获取客服与客户聊天记录
     * @param int $startTime 开始时间 时间戳
     * @param int $endTime 结束时间 时间戳
     * @param int $msgId 消息ID
     * @param int $number 数量
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/customer_service
     */
    public static function messages(int $startTime, int $endTime, int $msgId = 1, int $number = 10000)
    {
        $result = self::getInstance()->customer_service->messages($startTime, $endTime, $msgId, $number);
        return Wechat::apiError($result);
    }

    /**
     * 主动发送消息给用户
     * @param string $message 消息内容
     * @param string $openId 目标用户OPENID
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/customer_service
     */
    public static function sendMessage(string $message, string $openId)
    {
        try{
            $result = self::getInstance()->customer_service->message($message)->to($openId)->send();
        }catch(\Exception $result){}
        return Wechat::apiError($result);
    }

    /**
     * 指定客服发送消息
     * @param string $message 消息内容
     * @param string $account 客服账号
     * @param string $openId 目标用户OPENID
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/customer_service
     */
    public static function sendMessageFrom(string $message, string $account, string $openId)
    {
        try{
            $result = self::getInstance()->customer_service->message($message)->from($account)->to($openId)->send();
        }catch(\Exception $result){}
        return Wechat::apiError($result);
    }

    /**
     * 邀请微信用户成为客服
     * @param string $account 客服账号
     * @param string $wxname 微信号
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/customer_service
     */
    public static function invite(string $account, string $wxname)
    {
        $result = self::getInstance()->customer_service->invite($account, $wxname);
        return Wechat::apiError($result);
    }

    /**
     * 创建会话
     * @param string $account 客服账号
     * @param string $openId 目标用户OPENID
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/customer_service
     */
    public static function sessionCreate(string $account, string $openId)
    {
        $result = self::getInstance()->customer_service_session->create($account, $openId);
        return Wechat::apiError($result);
    }

    /**
     * 关闭会话
     * @param string $account 客服账号
     * @param string $openId 目标用户OPENID
     * @return array|bool|void
     */
    public static function sessionClose(string $account, string $openId)
    {
        $result = self::getInstance()->customer_service_session->close($account, $openId);
        return Wechat::apiError($result);
    }

    /**
     * 获取客户会话状态
     * @param string $openId 目标用户OPENID
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/customer_service
     */
    public static function sessionGet(string $openId)
    {
        $result = self::getInstance()->customer_service_session->get($openId);
        return Wechat::apiError($result);
    }

    /**
     * 获取客服会话列表
     * @param string $account 客服账号
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/customer_service
     */
    public static function sessionList(string $account)
    {
        $result = self::getInstance()->customer_service_session->list($account);
        return Wechat::apiError($result);
    }

    /**
     * 获取未接入会话列表
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/customer_service
     */
    public static function sessionWaiting()
    {
        $result = self::getInstance()->customer_service_session->waiting();
        return Wechat::apiError($result);
    }

}